<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 规格组ajax类
 * @package  	ajax
 * @subpackage  backend
 * @author   	allen.qiang
 */

quanli_require('module/base/goodsHandler.class.php');

class ajax_specGroup {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询商品规格组
                    $this->searchSpecGroup();
                    break;
			    case 2: //添加和修改商品规格组
                    $this->saveSpecGroup();
                    break;
				case 4: //删除商品规格组
					$this->deleteSpecGroup();
					break;

                case 5: //待编辑规格组查询
                    $this->searchOneSpecGroup();
                    break;

                case 6: //查询最大序列号
                    $this->searchSort();
                    break;

                case 7: //更改使用状态
                    $this->changeUse();
                    break;
            }

        }

    }

    /**
     * 查询商品规格组
     */
    private function searchSpecGroup() {
        global $G_PGLOB;
        
        //参数
        $sortname  = $_REQUEST['sortname'] ;
        $sortorder = $_REQUEST['sortorder'] ;
        $page	   = $_REQUEST['page'] ;
        $rp		   = $_REQUEST['rp'] ;
        $specGroupName = trim($_REQUEST['specGroupName']) ;
        
        //查询条件
		$arg_array = array(
			'sortname' 	=> $sortname , 
			'sortorder' => $sortorder , 
			'page' 		=> $page , 
			'rp' 		=> $rp 
		);
		//字段查询方式
		$extend_array = array() ;
		if( $specGroupName != '' ) {
			$arg_array['specGroupName'] 	= $specGroupName ;
			$extend_array ['specGroupName'] = 1  ; 
		}
		
		$commHandler = new commHandler( $G_PGLOB->DBCA ) ;
		$result_data = $commHandler->search( $arg_array , 'goods_spec_group' , $extend_array ) ;
		$result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );
        
		if ($result_data) {
			$edit_permission = $G_PGLOB->has_permission(509);
            foreach($result_data as &$d_l) {
        		if($d_l->isColor == 0){
        			$d_l->isColor = '否';
        		}else{
        			$d_l->isColor = '是';
        		}

        		if($edit_permission){
            		$result_array['rows'][] = array(
	                    'id' 	=> $d_l->specGroupID,
                        'cell' 	=> array(
                            '<a href="javascript: void(0);" onclick="specGroup.edit(' . $d_l->specGroupID . ')">修改</a>&nbsp;' ,
                            $d_l->specGroupID ,
                            '&nbsp;&nbsp;&nbsp;&nbsp;' . $d_l->specGroupName,
                            $d_l->sortOrder,
                            $d_l->isColor,
                            $d_l->useFlg == 1 ?
                                '<a href="javascript: void(0);" onclick="specGroup.change.confirm(' . $d_l->specGroupID . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="specGroup.change.confirm(' . $d_l->specGroupID . ', 1 ) " >禁用</a>'

                        )
                    );
                }else{
                    $result_array['rows'][] = array(
                        'id' 	=> $d_l->specGroupID,
                        'cell' 	=> array(
                            $d_l->specGroupID ,
                            '&nbsp;&nbsp;&nbsp;&nbsp;' . $d_l->specGroupName,
                            $d_l->sortOrder,
                            $d_l->isColor,
                            $d_l->useFlg == 1 ? '启用' :'禁用'
                        )
                    );
                }
            }
        }
        echo json_encode( $result_array );
    }
    
    /**
     * 规格组保存和更新
     *
     */
    private function saveSpecGroup () {

    	global $G_PGLOB;
    	
    	$_REQUEST['creator'] = $G_PGLOB->currentUserID;
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->saveSpecGroupInfo( $_REQUEST ) ;
    	echo json_encode($result_array);
    }
    
	/**
	 * 删除规格组
	 *
	 */
	private function deleteSpecGroup () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->deleteSpecGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}
	/**
	 * 修改规格组的使用状态
	 *
	 */
	private function actionSpecGroup () {
		global $G_PGLOB;
		
    	$goodsHandler = new goodsHandler( $G_PGLOB->DBCA ) ;
    	$result_array = $goodsHandler->actionSpecGroup( $_REQUEST ) ;
    	echo json_encode($result_array);
	}

    /**
     * 待编辑商品规格组查询
     */
    private function searchOneSpecGroup() {
        global $G_PGLOB;

        //查询条件
        $arg_array = array(
            'specGroupID' 	=> $_REQUEST['specGroupID']
        );

        $commHandler = new commHandler( $G_PGLOB->DBCA ) ;
        $result_data = $commHandler->search( $arg_array , 'goods_spec_group'  ) ;
        $result_array = array(
            'specGroupID'   => $result_data[0]->specGroupID,
            'specGroupName' => $result_data[0]->specGroupName ,
            'sortOrder'     => $result_data[0]->sortOrder ,
            'useFlg'        => $result_data[0]->useFlg ,
            'isColor'       => $result_data[0]->isColor ,

        );

        echo json_encode( $result_array );
    }

    //查询排序号
    private function searchSort(){

        global $G_PGLOB;
        //实例化系统公共类获取最大序列号
        $commHandler = new commHandler( $G_PGLOB->DBCA );
        $sort = $commHandler->getSortNum("goods_spec_group","sortOrder");
        $result = array('sortOrder'=>$sort);
        echo json_encode($result);
    }

    //更改使用状态
    private function changeUse(){
        global $G_PGLOB;
        //实例化系统公共类获取最大序列号
        $commHandler = new goodsHandler( $G_PGLOB->DBCA );
        $result = $commHandler->changeUse($_REQUEST);

        echo json_encode($result);
    }
}
